import dayjs from "dayjs";
import { getDate } from "@/utils/index";

const initPanelsLayout = [
  {
    backgroundImageId: "",
    backgroundImageUrl: "",
    createTime: "2024-02-04T03:15:03.840Z",
    createdUserId: "",
    createdUserName: "",
    dataBoardList: "",
    dataBoards: [
      {
        x: 0,
        y: 0,
        w: 16,
        h: 5,
        name: "WaitMatter",
        i: "0", // 唯一序号
        type: 1, // 类型 1：内部组件  2：外部报表
        url: "demo/gridLayoutUse/components/WaitMatter",
      },
      {
        x: 0,
        y: 0,
        w: 12,
        h: 5,
        name: "TemplateError",
        i: "1",
        type: 2,
        url: "http://192.168.3.5:8011/system/swagger-ui.html#/%E5%B7%A5%E4%BD%9C%E5%8F%B0%E9%A1%B5%E9%9D%A2/saveUsingPOST_22",
      },
    ],
    deleted: 0,
    id: "",
    ids: [""],
    lastUpdateTime: "2024-02-04T03:15:03.840Z",
    lastUpdatedUserId: "",
    lastUpdatedUserName: "",
    pageContent: "",
    pageName: "设备",
    pageObject: {},
    pageSeq: "",
    remarks: "",
    version: 0,
    workbenchId: "",
  },
  {
    backgroundImageId: "",
    backgroundImageUrl: "",
    createTime: "2024-02-04T03:15:03.840Z",
    createdUserId: "",
    createdUserName: "",
    dataBoardList: "",
    dataBoards: [
      {
        x: 0,
        y: 0,
        w: 8,
        h: 10,
        name: "WaitMatter",
        i: "0",
        type: 2,
        url: "https://www.makeapie.cn/echarts_content/x0oZWoncE.html",
      },
    ],
    deleted: 0,
    id: "",
    ids: [""],
    lastUpdateTime: "2024-02-04T03:15:03.840Z",
    lastUpdatedUserId: "",
    lastUpdatedUserName: "",
    pageContent: "",
    pageName: "质量",
    pageObject: {},
    pageSeq: "",
    remarks: "",
    version: 0,
    workbenchId: "",
  },
];

// 组件默认位置   name为组件名称
const initPositionLayout = [
  {
    x: 0,
    y: 0,
    w: 10,
    h: 8,
    i: 8,
    name: "外部报表",
    static: true,
  },
  { x: 0, y: 0, w: 17, h: 11, i: 9, name: "WaitMatter" },
  { x: 0, y: 0, w: 24, h: 8, i: 10, name: "ReportHeader" },
  { x: 0, y: 0, w: 8, h: 14, i: 10, name: "MessageBoard" },
  { x: 0, y: 0, w: 8, h: 14, i: 10, name: "NoticeBoard" },
  { x: 0, y: 0, w: 16, h: 18, i: 10, name: "BarLine" },
  { x: 0, y: 0, w: 16, h: 15, i: 10, name: "Pies" },
  { x: 0, y: 0, w: 16, h: 15, i: 10, name: "PieReport" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "LightLine" },
  { x: 0, y: 0, w: 10, h: 14, i: 10, name: "Gauge" },
  { x: 0, y: 0, w: 11, h: 22, i: 10, name: "Calendar" },
  { x: 0, y: 0, w: 12, h: 11, i: 10, name: "LiquidFill" },
  { x: 0, y: 0, w: 9, h: 14, i: 10, name: "ReferCard" },
  { x: 0, y: 0, w: 24, h: 30, i: 10, name: "FineReport" },
  { x: 0, y: 0, w: 24, h: 29, i: 10, name: "Gannt" },
  { x: 0, y: 0, w: 24, h: 32, i: 10, name: "ProduceLine" },
  { x: 0, y: 0, w: 24, h: 29, i: 10, name: "DrawingBoard" },
  { x: 0, y: 0, w: 8, h: 14, i: 10, name: "RemindBoard" },
  { x: 0, y: 0, w: 24, h: 29, i: 10, name: "WorkGanntBoard" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "DeviceRepairModePie" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "DeviceRepairNoStopBar" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "DeviceRepairPositionBar" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "DeviceRepairStopBar" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "DeviceRepairWorkShopBar" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "DeviceRepairYearLine" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "QmsBatchRejectBar" },
  { x: 0, y: 0, w: 24, h: 9, i: 10, name: "CommonMenuVisitCard" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "QmsNoGoodRejectBar" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "QmsProductRejectBar" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "QmsReturnRepairBar" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "QmsWeightProduct" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "QmsWeightProcess" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "QmsProductTypeReject" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "QmsDayReject" },
  { x: 0, y: 0, w: 24, h: 25, i: 10, name: "StackedBar" },
  { x: 0, y: 0, w: 12, h: 20, i: 10, name: "ProductionOverallSummaryRoders" },
  { x: 0, y: 0, w: 12, h: 20, i: 10, name: "ProductionDeliveryScheduleAll" },
  { x: 0, y: 0, w: 12, h: 20, i: 10, name: "ProductionDeliveryScheduleOut" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "DeviceMplanAchieveRate" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "ProductionSchedulingAnalysis" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "ProductionSchedulingTime" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "ProductionSchedulingPlan" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "ProductionPolishAnalysis" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "ProductionPolishInner" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "ProductionPolishOutside" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "ProductionMixAnalysis" },
  { x: 0, y: 0, w: 12, h: 32, i: 10, name: "ProductionTopWeight" },
  { x: 0, y: 0, w: 12, h: 32, i: 10, name: "ProductionTopQuantity" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "ProductionChipMakerNumber" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "ProductionCleanEquipment" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "ProductionAttendanceHours" },
  { x: 0, y: 0, w: 12, h: 14, i: 10, name: "ProductionLaborHour" },
];

// 获取默认看板位置
const getPosition = (baseName: string) => {
  const arr = initPositionLayout.filter(
    (item: initPositionObject) =>
      item.name === baseName || item.name === item.databoradName
  );
  return arr;
};

// 获取最新看板Object
const getPanelObject = (
  singlePanel: object,
  arr: object,
  item: any,
  i = "0",
  component: any,
  DemoIframeReport: any,
  workbenchId: any,
  isRadio: boolean
) => {
  const object: any = {
    ...singlePanel,
    ...arr,
    component:
      item.databoradType === "1"
        ? shallowRef(component)
        : shallowRef(DemoIframeReport),
    url: item.databoradUrl,
    name: item.databoradName,
    type: item.databoradType,
    i,
    params: item.databoradParam
      ? { ...JSON.parse(item.databoradParam), workbenchId }
      : { workbenchId },
  };
  if (isRadio) {
    object["w"] = 24;
  }
  return object;
};

const filterSaveTemplate = (data: PageObjectItem<PageObjectBoards>[]) => {
  const pageData = JSON.parse(JSON.stringify(data));
  pageData.forEach((item: PageObjectItem<PageObjectBoards>) => {
    item.pageObject?.Boards?.forEach((self: BoardsObject) => {
      delete self.component;
    });
  });
  return pageData;
};

const singlePanel = {
  x: 0,
  y: 0,
  w: 10,
  h: 8,
  i: 8,
  name: "",
  static: false,
};

interface initPositionObject {
  x?: number;
  y?: number;
  w?: number;
  h?: number;
  i?: number;
  name?: string;
  static?: boolean;
  databoradName?: string;
}

interface PageObjectItem<T> {
  pageObject?: T;
}

interface PageObjectBoards {
  Boards?: BoardsObject[];
}

interface BoardsObject {
  url: string;
  type?: string | undefined;
  component?: any;
  i?: string;
  name?: string;
  databoradUrl?: string;
}

const ganttTasks = [
  {
    id: 1,
    label: "Make some noise",
    user: '<a href="https://www.google.com/search?q=John+Doe" target="_blank" style="color:#0077c0;">John Doe</a>',
    start: getDate(-24 * 5),
    duration: 15 * 24 * 60 * 60 * 1000,
    percent: 85,
    type: "project",
    //collapsed: true,
  },
  {
    id: 2,
    label: "With great power comes great responsibility",
    user: '<a href="https://www.google.com/search?q=Peter+Parker" target="_blank" style="color:#0077c0;">Peter Parker</a>',
    parentId: 1,
    start: getDate(-24 * 4),
    duration: 4 * 24 * 60 * 60 * 1000,
    percent: 50,
    type: "milestone",
    collapsed: true,
    style: {
      base: {
        fill: "#1EBC61",
        stroke: "#0EAC51",
      },
    },
  },
  {
    id: 3,
    label: "Courage is being scared to death, but saddling up anyway.",
    user: '<a href="https://www.google.com/search?q=John+Wayne" target="_blank" style="color:#0077c0;">John Wayne</a>',
    parentId: 2,
    start: getDate(-24 * 3),
    duration: 2 * 24 * 60 * 60 * 1000,
    percent: 100,
    type: "task",
  },
  {
    id: 4,
    label: "Put that toy AWAY!",
    user: '<a href="https://www.google.com/search?q=Clark+Kent" target="_blank" style="color:#0077c0;">Clark Kent</a>',
    start: getDate(-24 * 2),
    duration: 2 * 24 * 60 * 60 * 1000,
    percent: 50,
    type: "task",
    dependentOn: [3],
  },
  {
    id: 5,
    label:
      "One billion, gajillion, fafillion... shabadylu...mil...shabady......uh, Yen.",
    user: '<a href="https://www.google.com/search?q=Austin+Powers" target="_blank" style="color:#0077c0;">Austin Powers</a>',
    parentId: 4,
    start: getDate(0),
    duration: 2 * 24 * 60 * 60 * 1000,
    percent: 10,
    type: "milestone",
    style: {
      base: {
        fill: "#0287D0",
        stroke: "#0077C0",
      },
    },
  },
  {
    id: 6,
    label: "Butch Mario and the Luigi Kid",
    user: '<a href="https://www.google.com/search?q=Mario+Bros" target="_blank" style="color:#0077c0;">Mario Bros</a>',
    parentId: 5,
    start: getDate(24),
    duration: 1 * 24 * 60 * 60 * 1000,
    percent: 50,
    type: "task",
    collapsed: true,
    style: {
      base: {
        fill: "#8E44AD",
        stroke: "#7E349D",
      },
    },
  },
  {
    id: 7,
    label: "Devon, the old man wanted me, it was his dying request",
    user: '<a href="https://www.google.com/search?q=Knight+Rider" target="_blank" style="color:#0077c0;">Knight Rider</a>',
    parentId: 2,
    dependentOn: [6],
    start: getDate(24 * 2),
    duration: 4 * 60 * 60 * 1000,
    percent: 20,
    type: "task",
    collapsed: true,
  },
  {
    id: 8,
    label: "Hey, Baby! Anybody ever tell you I have beautiful eyes?",
    user: '<a href="https://www.google.com/search?q=Johhny+Bravo" target="_blank" style="color:#0077c0;">Johhny Bravo</a>',
    parentId: 7,
    dependentOn: [7],
    start: getDate(24 * 3),
    duration: 1 * 24 * 60 * 60 * 1000,
    percent: 0,
    type: "task",
  },
  {
    id: 9,
    label:
      "This better be important, woman. You are interrupting my very delicate calculations.",
    user: '<a href="https://www.google.com/search?q=Dexter\'s+Laboratory" target="_blank" style="color:#0077c0;">Dexter\'s Laboratory</a>',
    parentId: 8,
    dependentOn: [8, 7],
    start: getDate(24 * 4),
    duration: 4 * 60 * 60 * 1000,
    percent: 20,
    type: "task",
    style: {
      base: {
        fill: "#8E44AD",
        stroke: "#7E349D",
      },
    },
  },
  {
    id: 10,
    label: "current task",
    user: '<a href="https://www.google.com/search?q=Johnattan+Owens" target="_blank" style="color:#0077c0;">Johnattan Owens</a>',
    start: getDate(24 * 5),
    duration: 24 * 60 * 60 * 1000,
    percent: 0,
    type: "task",
  },
  {
    id: 11,
    label: "test task",
    user: '<a href="https://www.google.com/search?q=Johnattan+Owens" target="_blank" style="color:#0077c0;">Johnattan Owens</a>',
    start: getDate(24 * 6),
    duration: 24 * 60 * 60 * 1000,
    percent: 0,
    type: "task",
  },
  {
    id: 12,
    label: "test task",
    user: '<a href="https://www.google.com/search?q=Johnattan+Owens" target="_blank" style="color:#0077c0;">Johnattan Owens</a>',
    start: getDate(24 * 7),
    duration: 24 * 60 * 60 * 1000,
    percent: 0,
    type: "task",
    parentId: 11,
  },
  {
    id: 13,
    label: "test task",
    user: '<a href="https://www.google.com/search?q=Johnattan+Owens" target="_blank" style="color:#0077c0;">Johnattan Owens</a>',
    start: getDate(24 * 8),
    duration: 24 * 60 * 60 * 1000,
    percent: 0,
    type: "task",
  },
  {
    id: 14,
    label: "test task",
    user: '<a href="https://www.google.com/search?q=Johnattan+Owens" target="_blank" style="color:#0077c0;">Johnattan Owens</a>',
    start: getDate(24 * 9),
    duration: 24 * 60 * 60 * 1000,
    percent: 0,
    type: "task",
  },
  {
    id: 15,
    label: "test task",
    user: '<a href="https://www.google.com/search?q=Johnattan+Owens" target="_blank" style="color:#0077c0;">Johnattan Owens</a>',
    start: getDate(24 * 16),
    duration: 24 * 60 * 60 * 1000,
    percent: 0,
    type: "task",
  },
];
const ganttOptions = {
  taskMapping: {
    progress: "percent",
  },
  maxRows: 100,
  maxHeight: 500,
  title: {
    label: "Your project title as html (link or whatever...)",
    html: false,
  },
  row: {
    height: 24,
  },
  calendar: {
    hour: {
      display: true,
    },
  },
  chart: {
    progress: {
      bar: false,
    },
    expander: {
      display: true,
    },
  },
  taskList: {
    expander: {
      straight: false,
    },
    columns: [
      {
        id: 1,
        label: "ID",
        value: "id",
        width: 40,
      },
      {
        id: 2,
        label: "Description",
        value: "label",
        width: 200,
        expander: true,
        html: true,
        events: {
          click({ data, column }) {
            alert("description clicked!\n" + data.label);
          },
        },
      },
      {
        id: 3,
        label: "Assigned to",
        value: "user",
        width: 130,
        html: true,
      },
      {
        id: 3,
        label: "Start",
        value: (task) => dayjs(task.start).format("YYYY-MM-DD"),
        width: 78,
      },
      {
        id: 4,
        label: "Type",
        value: "type",
        width: 68,
      },
      {
        id: 5,
        label: "%",
        value: "progress",
        width: 35,
        style: {
          "task-list-header-label": {
            "text-align": "center",
            width: "100%",
          },
          "task-list-item-value-container": {
            "text-align": "center",
            width: "100%",
          },
        },
      },
    ],
  },
  locale: {
    name: "en",
    Now: "Now",
    "X-Scale": "Zoom-X",
    "Y-Scale": "Zoom-Y",
    "Task list width": "Task list",
    "Before/After": "Expand",
    "Display task list": "Task list",
  },
};

export {
  ganttTasks,
  ganttOptions,
  filterSaveTemplate,
  getPanelObject,
  getPosition,
  initPanelsLayout,
  initPositionLayout,
  singlePanel,
  initPositionObject,
  PageObjectItem,
  BoardsObject,
  PageObjectBoards,
};
